<template>
  <PageWrapper dense contentFullHeight>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <!-- <a-button
          type="primary"
          color="error"
          v-hasAnyPermission="[RoleEnum.TENANT_USER_DELETE]"
          @click="handleBatchDelete"
          >{{ t('common.title.delete') }}
        </a-button> -->
        <!-- <a-button v-hasAnyPermission="['timeUpload:task']" type="primary" @click="openModal2"
          >设置资料上传截止时间</a-button
        > -->
        <a-button type="primary" v-hasAnyPermission="[RoleEnum.TENANT_USER_ADDS]" @click="handleAdd"
          >{{ t('common.title.add') }}
        </a-button>
        <a-button
          preIcon="ant-design:download-outlined"
          type="primary"
          v-hasAnyPermission="['tfc:download']"
          @click="handleBatchDownload()"
        >
          下载特色贡献清单
        </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'action'" class="fathes">
          <div class="xingqing">
            <TableAction
              :actions="[
                {
                  label: t('common.title.view'),
                  onClick: handleView.bind(null, record),
                  auth: RoleEnum.TENANT_USER_VIEWS,
                },
              ]"
              :dropDownActions="[
                // {
                //   label: '重置密码',
                //   icon: 'ant-design:rest-filled',
                //   onClick: handleResetPwd.bind(null, record),
                //   auth: RoleEnum.TENANT_USER_RESET_PWD,
                // },
                {
                  label: t('common.title.edit'),
                  icon: 'ant-design:edit-outlined',
                  onClick: handleEdit.bind(null, record),
                  auth: RoleEnum.TENANT_USER_EDITS,
                },
                {
                  label: '评价',
                  icon: 'ant-design:edit-outlined',
                  onClick: handleEdits.bind(null, record),
                  auth: RoleEnum.TENANT_USER_PingFEn,
                },
                // {
                //   label: t('common.title.copy'),
                //   icon: 'ant-design:copy-outlined',
                //   onClick: handleCopy.bind(null, record),
                //   auth: RoleEnum.TENANT_USER_ADD,
                // },
                // {
                //   label: t('common.title.delete'),
                //   icon: 'ant-design:delete-outlined',
                //   auth: RoleEnum.TENANT_USER_DELETE,
                //   popConfirm: {
                //     title: t('common.tips.confirmDelete'),
                //     confirm: handleDelete.bind(null, record),
                //   },
                // },
              ]"
            />
          </div>
          <div class="nexts">
            <BasicUpload
              :maxSize="200"
              :maxNumber="10"
              @change="handleChanged"
              @click="dianji(record)"
              :api="UploadPdf"
              class="my-5"
              :accept="['pdf']"
              :records="record.id"
              v-if="upLoadShow != '1'"
            />
          </div>
        </template>
      </template>
    </BasicTable>
    <EditModal @register="registerDrawer" @success="handleSuccess" :foundId="upLoadShow" />
    <Model @register="registerDown" @success="SuccessDown" />
    <!-- <RestPasswordModal @register="registerModal" @success="handleSuccess" /> -->
  </PageWrapper>
</template>
<script lang="ts">
  import { defineComponent, ref, unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { PageWrapper } from '/@/components/Page';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { handleFetchParams } from '/@/utils/lamp/common';
  import { ActionEnum } from '/@/enums/commonEnum';
  import { remove } from '/@/api/devOperation/tenant/defUser';
  import { getTaskFeatureContributeList, TsUpload, UploadPdf } from '/@/api/basic/qimo/index';
  import { columns, searchFormSchema } from './defUser.data';
  import { RoleEnum } from '/@/enums/roleEnum';
  import EditModal from './Edit.vue';
  import Model from './Model.vue';
  import { useUserStore } from '/@/store/modules/user';
  import RestPasswordModal from './RestPassword.vue';
  import { UploadPdfResult } from '/@/api/basic/qimo/model/index';
  import BasicUpload from '../Upload/src/BasicUpload.vue';
  export default defineComponent({
    // 若需要开启页面缓存，请将此参数跟菜单名保持一致
    name: '用户维护',
    components: {
      BasicTable,
      PageWrapper,
      Model,
      EditModal,
      TableAction,
      BasicUpload,
    },
    setup() {
      const { t } = useI18n();
      const { createMessage, createConfirm } = useMessage();
      // 编辑页弹窗
      const [registerDrawer, { openDrawer }] = useDrawer();
      const [registerDown, { openModal }] = useModal();
      // const [registerModal, { openModal}] = useModal();
      const userStore = useUserStore();
      const upLoadShow = userStore.getTenantId;
      const [registered, { openModals }] = useModal();
      // 表格
      const [registerTable, { reload, getSelectRowKeys }] = useTable({
        title: '特色贡献列表',
        api: getTaskFeatureContributeList,
        columns: columns(),
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema(),
        },
        beforeFetch: handleFetchParams,
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        rowKey: 'id',
        rowSelection: {
          type: 'checkbox',
        },
        actionColumn: {
          width: 180,
          title: t('common.column.action'),
          dataIndex: 'action',
        },
        // titleHelpMessage: [
        //   '1. 新增用户只存def_user表',
        //   '2. 初始密码统一为：123456',
        //   '3. 可以使用用户名、手机、邮箱、身份证登录',
        //   '4. 用户禁用后，将不能登录系统',
        // ],
      });

      // 弹出复制页面
      function handleCopy(record: Recordable, e: Event) {
        e?.stopPropagation();
        openDrawer(true, {
          record,
          type: ActionEnum.COPY,
        });
      }

      // 弹出查看页面
      function handleView(record: Recordable, e: Event) {
        e?.stopPropagation();
        openDrawer(true, {
          record,
          type: ActionEnum.VIEW,
        });
      }

      // 弹出新增页面
      function handleAdd() {
        openDrawer(true, {
          type: ActionEnum.ADD,
        });
      }

      // 弹出编辑页面
      function handleEdit(record: Recordable, e: Event) {
        e?.stopPropagation();
        openDrawer(true, {
          record,
          type: ActionEnum.EDIT,
        });
        // reload();
      }
      // 弹出编辑评价页面
      function handleEdits(record: Recordable, e: Event) {
        e?.stopPropagation();
        openDrawer(true, {
          record,
          type: ActionEnum.PingFEn,
        });
      }

      // 新增或编辑成功回调
      function handleSuccess() {
        reload();
      }

      async function batchDelete(ids: any[]) {
        await remove(ids);
        createMessage.success(t('common.tips.deleteSuccess'));
        handleSuccess();
      }

      // 点击单行删除
      function handleDelete(record: Recordable, e: Event) {
        e?.stopPropagation();
        if (record?.id) {
          batchDelete([record.id]);
        }
      }

      // 重置密码
      function handleResetPwd(record: Recordable) {
        openModal(true, { record });
      }

      // 点击批量删除
      function handleBatchDelete() {
        const ids = getSelectRowKeys();
        if (!ids || ids.length <= 0) {
          createMessage.warning(t('common.tips.pleaseSelectTheData'));
          return;
        }
        createConfirm({
          iconType: 'warning',
          content: t('common.tips.confirmDelete'),
          onOk: async () => {
            await batchDelete(ids);
          },
        });
      }
      const fileList = ref<UploadPdfResult[] | any[]>([]);
      const uploadID = ref();
      // 评分
      function handleChanges(files: UploadPdfResult[]) {
        fileList.value = [...unref(fileList), ...(files || [])];
        const newUpload: string[] = [];
        console.log(files);
        for (var i = 0; i < files.length; i++) {
          const list = [files[i].id];
          const StringList = String(list);
          newUpload.push(StringList);
        }
      }
      function dianji(record) {
        uploadID.value = record;
      }
      // 上传Pdf
      async function taskInterimMaterial(fileIds) {
        // console.log(fileIds, '2222222222');
        const result = await TsUpload(fileIds);
        reload();
        return result;
      }
      function handleChanged(files: UploadPdfResult[]) {
        fileList.value = [...unref(fileList), ...(files || [])];
        const newUpload: string[] = [];
        for (var i = 0; i < files.length; i++) {
          const list = [files[i].id];
          const StringList = String(list);
          newUpload.push(StringList);
        }

        const arrs = {
          tenantId: uploadID.value.tenantId,
          // tenantId: '526891104516526764',
          fileIds: newUpload,
          tfcId: uploadID.value.id,
          remarks: uploadID.value.remarks,
        };
        taskInterimMaterial(arrs);
        reload();
        // JSON.stringify(params)
        return createMessage.warning(t('component.upload.sessuess'));
      }
      // 下载
      function handleBatchDownload() {
        openModal(true, {});
      }

      function SuccessDown() {
        createMessage.success('下载成功');
      }
      return {
        t,
        dianji,
        UploadPdf,
        TsUpload,
        handleChanges,
        handleChanged,
        registerTable,
        registerDrawer,
        handleAdd,
        upLoadShow,
        handleCopy,
        handleView,
        handleEdit,
        handleDelete,
        handleSuccess,
        handleBatchDelete,
        handleResetPwd,
        RoleEnum,
        // registerModal,
        handleEdits,
        handleBatchDownload,
        registerDown,
        SuccessDown,
      };
    },
  });
</script>
<style>
  .fathes {
    position: relative;
  }

  .nexts {
    position: absolute;
    top: -3px;
    left: 87px;
  }

  .xingqing {
    position: absolute;
    top: 22px;
    left: 5px;
  }

  .popup {
    display: none;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: white;
    padding: 20px;
    border: 1px solid #ccc;
    box-shadow: 0 0 10px rgb(0 0 0 / 30%);
  }
</style>
